<?php

namespace App\Model;

use EasySwoole\DDL\Enum\Character;
use EasySwoole\DDL\Enum\Engine;
use EasySwoole\ORM\AbstractModel;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\ORM\Utility\Schema\Table;

class OrderModel extends AbstractModel
{
    protected $tableName = 't_order';
    protected $autoTimeStamp = true; // 自动写入create_time和update_time 这两个字段是int

    /**
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getOrder($where = ['is_deleted' => 0])
    {
        $where['is_deleted'] = 0;
        return $this->order('id', 'desc')->get($where);
    }

    /**
     * @throws Throwable
     * @throws Exception
     */
    public function addOrder($where)
    {
        $this->data($where, false);
        return $this->save();
    }

    public function schemaInfo(bool $isCache = true): Table
    {
        $table = new Table($this->tableName);

        $table->setIfNotExists();
        $table->setTableEngine(Engine::INNODB);
        $table->setTableCharset(Character::UTF8MB4_GENERAL_CI);
        $table->setTableComment('订单表');

        $table->colInt('id')->setIsAutoIncrement()->setIsPrimaryKey(true)->setColumnComment("ID");
        $table->colInt('uid', 25)->setDefaultValue(0)->setIsNotNull()->setColumnComment('用户ID');
        $table->colInt('type', 25)->setDefaultValue(0)->setIsNotNull()->setColumnComment('类型：0.待定，1.物业费， 2.车位费， 3.报事报修， 4.活动');
        $table->colInt('rid', 25)->setDefaultValue(0)->setColumnComment('关联id');
        $table->colVarChar('order_no', 32)->setDefaultValue('')->setColumnComment('订单号');
        $table->colDouble('amount_real', 8, 2)->setDefaultValue('0.00')->setColumnComment('实际金额');
        $table->colInt('payment_type', 5)->setDefaultValue(0)->setColumnComment('支付平台');
        $table->colVarChar('payment_trade', 64)->setDefaultValue('')->setColumnComment('支付单号');
        $table->colInt('payment_status', 1)->setDefaultValue(0)->setColumnComment('支付状态');
        $table->colDouble('payment_amount', 8, 2)->setDefaultValue('0.00')->setColumnComment('实际支付金额');
        $table->colVarChar('payment_remark', 500)->setDefaultValue('')->setColumnComment('支付结果描述');
        $table->colInt('payment_time', 10)->setDefaultValue(0)->setColumnComment('支付时间');
        $table->colVarChar('order_remark', 100)->setDefaultValue('')->setColumnComment('订单用户备注');
        $table->colInt('status', 2)->setDefaultValue(0)->setColumnComment('订单状态：(0已取消,1预订单,2待支付,3支付中,4已支付,5已发货,6已完成)');
        $table->colInt('sort', 4)->setDefaultValue(99)->setColumnComment('权重');
        $table->colTinyInt('is_deleted', 1)->setDefaultValue(0)->setIsNotNull()->setColumnComment('是否删除：0.未删除，1.已删除');
        $table->colInt('create_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('创建时间戳');
        $table->colInt('update_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('更新时间戳');

        $table->indexNormal('uid_index', 'uid');
        $table->indexNormal('rid_index', 'rid');
        $table->indexNormal('amount_real_index', 'amount_real');
        $table->indexNormal('payment_type_index', 'payment_type');
        $table->indexNormal('payment_amount_index', 'payment_amount');
        $table->indexNormal('payment_time_index', 'payment_time');
        $table->indexNormal('status_index', 'status');

        return $table;
    }
}